package cn.bestick.cblog.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.bestick.cblog.dmo.Msg;
import cn.bestick.cblog.dmo.User;
import cn.bestick.cblog.dto.MsgDto;
import cn.bestick.cblog.dto.UserDto;
import cn.bestick.cblog.service.MsgService;
import cn.bestick.cblog.service.UserService;
import cn.bestick.cblog.service.impl.MsgServiceImpl;
import cn.bestick.cblog.service.impl.UserServiceImpl;

public class MsgServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private MsgService msgService = new MsgServiceImpl();
	private UserService userService = new UserServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.service(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.service(request, response);
	}

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		if ("save".equals(action)) {
			save(request, response);
		} else if ("leaveMsg".equals(action)) {
			leaveMsg(request, response);
		} else {
			request.getRequestDispatcher("home").forward(request, response);
		}
	}

	private void leaveMsg(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<MsgDto> leaveMsgs = msgService.getLeaveMsgs();
		request.setAttribute("type", 1);
		request.setAttribute("replytype", 3);
		request.setAttribute("pid", 0);
		request.setAttribute("articleId", 0);
		request.setAttribute("msgs", leaveMsgs);
		request.setAttribute("mainPage", "template/leavemsg/detail.jsp");
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}

	private void save(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String nickname = request.getParameter("nickname");
		String email = request.getParameter("email");
		String content = request.getParameter("content");
		String type = request.getParameter("type");
		String pid = request.getParameter("pid");
		String articleId = request.getParameter("articleId");
		HttpSession session = request.getSession();
		UserDto currentUser = (UserDto) session.getAttribute("currentUser");

		if ("".equals(nickname) || "".equals(content) || "".equals(email)) {
			request.setAttribute("error", "昵称、邮箱和内容不可为空");
			List<MsgDto> leaveMsgs = msgService.getLeaveMsgs();
			request.setAttribute("type", 1);
			request.setAttribute("pid", 0);
			request.setAttribute("msgs", leaveMsgs);
			request.setAttribute("mainPage", "template/leavemsg/detail.jsp");
			request.getRequestDispatcher("index.jsp")
					.forward(request, response);
		} else if (currentUser == null) {
			String website = request.getParameter("website");
			User user = new User();
			user.setUsername(nickname);
			user.setPassword(email);
			currentUser = userService.login(user);
			if (currentUser == null) {
				user.setImageName("default.jpg");
				user.setNickname(nickname);
				user.setEmail(email);
				user.setWebsite(website);
				userService.saveUser(user);
				currentUser = userService.login(user);
			}

			int userId = currentUser.getId();

			Msg msg = new Msg();
			msg.setUserId(userId);
			msg.setContent(content);
			msg.setType(Integer.parseInt(type));
			msg.setPid(Integer.parseInt(pid));
			msgService.saveMsg(msg);

			int IType = Integer.parseInt(type);
			if (IType % 2 != 0) {
				response.sendRedirect("msg?action=leaveMsg");
			} else {
				response.sendRedirect("article?articleId=" + articleId);
			}
		} else {
			int userId = currentUser.getId();

			Msg msg = new Msg();
			msg.setUserId(userId);
			msg.setContent(content);
			msg.setType(Integer.parseInt(type));
			msg.setPid(Integer.parseInt(pid));
			msgService.saveMsg(msg);

			int IType = Integer.parseInt(type);
			if (IType % 2 != 0) {
				response.sendRedirect("msg?action=leaveMsg");
			} else {
				response.sendRedirect("article?articleId=" + articleId);
			}
		}
	}
}
